#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
from tkinter import StringVar, Button

from base.BaseSubWindow import BaseSubWindow
from sdautolayout.sdautolayout import sdautolayout
from utils.BrowserUtils import BrowserUtils
from widget.ComboboxExt import ComboboxExt


class AccountWindow(BaseSubWindow):
    def __init__(self, root):
        super(AccountWindow, self).__init__(root=root, width=460, height=50)
        self.title("账号集锦")
        self.init_ui()
        self.init_data()

    def init_ui(self):
        self.str_channel = StringVar()
        self.cbx_channel = ComboboxExt(self, textvariable=self.str_channel)
        self.cbx_channel.place(x=5, y=5)
        sdautolayout.forWidget(self.cbx_channel).widthIs(100).heightIs(30)
        self.cbx_channel["state"] = "readonly"

        self.str_account = StringVar()
        self.cbx_account = ComboboxExt(self, textvariable=self.str_account)
        self.cbx_account.place(x=5, y=5)
        sdautolayout.forWidget(self.cbx_account).widthIs(170).heightIs(30).leftSpaceToView(self.cbx_channel, 5)
        self.cbx_account["state"] = "readonly"

        self.button_page = Button(self, text='登录页面', command=self.loginPage)
        self.button_page.place(x=5, y=5)
        sdautolayout.forWidget(self.button_page).widthIs(80).leftSpaceToView(self.cbx_account, 5)

        self.button_fill_and_login = Button(self, text='填充', command=self.fill)
        self.button_fill_and_login.place(x=5, y=5)
        sdautolayout.forWidget(self.button_fill_and_login).widthIs(80).leftSpaceToView(self.button_page, 5)


    def loginPage(self):
        BrowserUtils.instance().load(self.dic_channel_url[self.cbx_channel.get()])


    def fill(self):
        account_id = self.dic_channel_account_id[self.cbx_channel.get()]
        account_value = self.cbx_account.get()

        password_id = self.dic_channel_password_id[self.cbx_channel.get()]
        password_value = self.cbx_account.value()

        if password_id.strip() == '':
            password_name = self.dic_channel_password_name[self.cbx_channel.get()]

        BrowserUtils.instance().set(id=account_id,value=account_value)

        if password_id.strip() == '':
            BrowserUtils.instance().setByName(name=password_name, value=password_value)
        else:
            BrowserUtils.instance().set(id=password_id, value=password_value)

    def currentDir(self):
        return os.path.split(os.path.realpath(__file__))[0];

    def init_data(self):
        settings = self.getSettings();
        self.dic_channel_url = {}
        self.dic_channel_account_id = {}
        self.dic_channel_password_id = {}
        self.dic_channel_password_name = {}
        dic_channel = {}
        for channel in settings["channel"]:
            dic_channel[channel["name"]] = channel["account"]
            self.dic_channel_url[channel["name"]] = channel["login_url"]
            self.dic_channel_account_id[channel["name"]] = channel["username_id"]
            self.dic_channel_password_id[channel["name"]] = channel["password_id"]
            self.dic_channel_password_name[channel["name"]] = channel["password_name"]


        self.cbx_channel.set_dict(dic_channel)
        self.cbx_channel.current(0)

        self.updateAccountCbx()
        self.cbx_channel.bind("<<ComboboxSelected>>", self.event_channel)

    def updateAccountCbx(self):
        dic_name_account = {}
        for account in self.cbx_channel.value():
            dic_name_account[account["username"]] = account["password"]
        self.cbx_account.set_dict(dic_name_account)
        self.cbx_account.current(0)

    def event_channel(self,event):
        self.updateAccountCbx()